/**
 * $Id: BookServiceFacadeImpl.java 24 2011-06-17 13:31:30Z feigme $
 */
package com.iary.application.impl;

import org.hibernate.criterion.Criterion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.iary.application.BookServiceFacade;
import com.iary.domain.model.book.Book;
import com.iary.domain.model.book.BookExistedException;
import com.iary.domain.model.book.BookRepository;
import com.iary.domain.model.book.chapter.BookChapter;
import com.iary.domain.model.book.chapter.BookChapterRepository;
import com.iary.domain.model.book.chapter.BookItem;
import com.iary.domain.service.BookService;
import com.iary.infrastructure.orm.Page;

/**
 * @author Feigme
 */
@Service
@Transactional
public class BookServiceFacadeImpl implements BookServiceFacade {

	private BookService bookService;
	private BookRepository bookRepository;
	private BookChapterRepository bookChapterRepository;

	BookServiceFacadeImpl() {

	}

	@Autowired
	public BookServiceFacadeImpl(BookService bookService,
			BookRepository bookRepository,
			BookChapterRepository bookChapterRepository) {
		this.bookService = bookService;
		this.bookRepository = bookRepository;
		this.bookChapterRepository = bookChapterRepository;
	}

	@Override
	public Book getBook(Long id) {
		Book book = bookRepository.get(id);
		book.getBookChapters();
		return book;
	}

	@Override
	public void save(Book book) throws BookExistedException {
		bookService.saveBook(book);
	}

	@Override
	public Book getLiteBook(Long id) {
		Book book = bookRepository.get(id);
		book.getBookDesc();
		return book;
	}

	@Override
	public Page<Book> findAll(Page<Book> page) {
		return bookService.findAll(page);
	}

	@Override
	public void delete(Long id) {
		Book book = bookRepository.get(id);
		if (book != null) {
			bookRepository.delete(id);
		}
	}

	@Override
	public Page<Book> findPage(Page<Book> page, Criterion... criterions) {
		return bookRepository.findPage(page, criterions);
	}

	@Override
	public void addBookChapter(Long id, BookChapter chapter) {
		Book book = bookRepository.get(id);
		book.addBookChapter(chapter);
	}

	@Override
	public BookChapter getBookChapter(Long id) {
		BookChapter bookChapter = bookChapterRepository.get(id);
		bookChapter.getChapterDesc();
		return bookChapter;
	}

	@Override
	public void updateBookChapter(Long id, BookChapter chapter) {
		BookChapter bookChapter = bookChapterRepository.get(id);
		bookChapter.setChapterDesc(chapter.getChapterDesc());
		bookChapter.setBookChapterType(chapter.getType());
		bookChapter.setOrder(chapter.getOrder());
		bookChapter.setPath(chapter.getPath());
	}

	@Override
	public void updateBook(Long id, Book book) throws BookExistedException {
		Book bookInDb = bookRepository.findBookByName(book.getName());
		if (bookInDb != null && !id.equals(bookInDb.getId())) {
			throw new BookExistedException("Existed book name : "
					+ book.getName());
		}
		bookInDb = bookRepository.get(id);
		bookInDb.setBookDesc(book.getBookDesc());
		bookInDb.setName(book.getName());
		bookInDb.setPath(book.getPath());
	}

	@Override
	public void updateCover(Long id, String cover) {
		Book bookInDb = bookRepository.get(id);
		bookInDb.setCover(cover);
	}

	@Override
	public void addBookItem(Long chapterId, BookItem item) {
		BookChapter chapter = bookChapterRepository.get(chapterId);
		chapter.addBookItem(item);
	}

	@Override
	public void updateBookItemOrder(Long chapterId, Long itemId, Double order) {
		BookChapter chapter = bookChapterRepository.get(chapterId);
		BookItem item = chapter.getBookItem(itemId);
		if (item != null) {
			item.setOrder(order);
		}
	}

	@Override
	public void deleteBookItemOrder(Long chapterId, Long itemId) {
		BookChapter chapter = bookChapterRepository.get(chapterId);
		chapter.removeBookItem(itemId);
	}
}
